package com.cs.service.impl;

import com.cs.common.Result;
import com.cs.dao.UserDao;
import com.cs.pojo.User;
import com.cs.service.UserService;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/9/12
 */
@Service
public class UserServiceImpl implements UserService {

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    private final UserDao userDao;

    @Override
    public Result login(String username, String password) {
        int count = userDao.checkUsername(username);
        if (count == 0) {
            return Result.createBySuccessMessage("用户不存在");
        }
        User user = userDao.selectLogin(username, password);
        if (user == null) {
            return Result.createByErrorMessage("密码错误");
        }
        return Result.createBySuccess();
    }

    @Override
    public Result selectUserByUserId(Integer userId) {
        User user = userDao.selectByPrimaryKey(userId);
        if (user == null) {
            return Result.createByErrorMessage("该用户不存在");
        }
        return Result.createBySuccess(user);
    }
}
